package game;

import gfx.Camera;
import gfx.Glyphs;
import gfx.PressableButton;

import java.text.DecimalFormat;

import javax.microedition.khronos.opengles.GL10;

import util.GameClock;
import util.GameVars;
import util.VertexBuffer;
import android.graphics.PointF;

public class Challenge {
	protected PressableButton[] pauseButtonArray;

	private PressableButton selectedButton;
	
	protected PressableButton retryButton;
	protected PressableButton quitButton;
	
	// 00000000
	DecimalFormat nf;

	private float[] overlay = new float[12];
	
	private short pauseButtonIterator;
	
	private boolean isLoaded;
	private boolean hasGraphicsLoaded;
	protected boolean isPaused = false;
	private boolean kill = false;
	
	protected boolean reloading = false;
	
	private boolean backToSelection = false;

	protected boolean isGameOver = false;
	protected boolean isWon = false;
	
	
	public Challenge(){
		load();
	}
	
	private void load(){
		isLoaded = false;
		isWon = false;
		isGameOver = false;
		
		// Resume and Quit
		pauseButtonArray = new PressableButton[2];
		pauseButtonArray[0] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Resume", null, 3, 0.75f, GameVars.BUTTON_NUM_RESUME);
		pauseButtonArray[1] = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Quit", null, 3, 0.75f, GameVars.BUTTON_NUM_GOTO_MENU);

		for(pauseButtonIterator = 0; pauseButtonIterator < pauseButtonArray.length; ++pauseButtonIterator)
			pauseButtonArray[pauseButtonIterator].setCenter(3f + pauseButtonIterator * 4, 4);
		
		retryButton = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Again!", null, 3, 0.75f, GameVars.BUTTON_NUM_RETRY);
		quitButton = new PressableButton(GameVars.BUTTON_TYPE_MENU, "Quit", null, 3, 0.75f, GameVars.BUTTON_NUM_QUIT);

		retryButton.setCenter(3, 4f);
		quitButton.setCenter(7, 4f);
		
		overlay[0] = 0;
		overlay[1] = 0;
		overlay[2] = 0;
		overlay[3] = 0;
		overlay[4] = Camera.SCREEN_HEIGHT;
		overlay[5] = 0;
		overlay[6] = Camera.SCREEN_WIDTH;
		overlay[7] = 0;
		overlay[8] = 0;
		overlay[9] = Camera.SCREEN_WIDTH;
		overlay[10] = Camera.SCREEN_HEIGHT;
		overlay[11] = 0;
		
		resume();
		
		isLoaded = true;
	}
	
	public void update(long time){
		if(reloading){
			reset();
			reloading = false;
			return;
		}
		
		if(isPaused)
			return;
		if(isGameOver)
			pause();
		if(isWon)
			pause();
	}
	
	public void draw(GL10 gl){
		if(isGameOver){
			drawOverlay(gl, .5f);
			retryButton.draw(gl);
			quitButton.draw(gl);
			
			gl.glPushMatrix();
			gl.glTranslatef(3, 8f, 0);
			gl.glScalef(2, 2, 1);
			gl.glColor4f(1, 1, 1, 1);
			Glyphs.drawString(gl, "Game Over", null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			
			return;
		}
		if(isWon){
			drawOverlay(gl, .5f);
			retryButton.draw(gl);
			quitButton.draw(gl);
			
			gl.glPushMatrix();
			gl.glTranslatef(5, 5f, 0);
			gl.glScalef(2, 2, 1);
			gl.glColor4f(1, 1, 1, 1);
			Glyphs.drawString(gl, "You Won!", null, GameVars.TEXT_ALIGNMENT_CENTER);
			gl.glPopMatrix();
			
			return;
		}
		if(isPaused){
			drawOverlay(gl, .75f);

			for(pauseButtonIterator = 0; pauseButtonIterator < pauseButtonArray.length; ++pauseButtonIterator)
				pauseButtonArray[pauseButtonIterator].draw(gl);
		}
	}
	
	private void drawOverlay(GL10 gl, float alpha){
		gl.glPushMatrix();
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, VertexBuffer.getVertexBuffer(overlay));

		gl.glColor4f(.0f, .0f, .0f, alpha);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, overlay.length / 3);

		gl.glColor4f(1, 1, 1, 1);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glPopMatrix();
	}
	
	public boolean isLoaded(){ return isLoaded; }
	public boolean hasGraphicsLoaded(){ return hasGraphicsLoaded; }
	public boolean kill(){ return kill; }
	public void setKill(boolean k){ kill = k; }
	public boolean isPaused(){ return isPaused; }
	public boolean back(){ return backToSelection; }
	
	public void pause(){ isPaused = true; GameClock.getInstance().pause(); }
	public void resume(){ isPaused = false; GameClock.getInstance().resume(); }

	private void buttonHandler(int num){
		switch(num){
		case GameVars.BUTTON_NUM_RETRY:
			reloading = true;
			break;
		case GameVars.BUTTON_NUM_RESUME:
			resume();
			break;
		case GameVars.BUTTON_NUM_QUIT:
			kill = true;
			break;
		case GameVars.BUTTON_NUM_GOTO_MENU:
			kill = true;
			break;
		case GameVars.BUTTON_NUM_RETURN_TO_SELECTION:
			backToSelection = true;
			break;
		}
	}

	public boolean handleUp(PointF location) {
		if(isWon){
			if(selectedButton != null){
				if(selectedButton.isPressed(location))
					buttonHandler(selectedButton.getNum());
				selectedButton = null;
				return true;
			}
		}
		if(isPaused){
			if(selectedButton != null){
				if(selectedButton.isPressed(location))
					buttonHandler(selectedButton.getNum());
				selectedButton = null;
				return true;
			}
		}
		return false;
	}

	public boolean handleDrag(PointF location) {
		return false;
	}

	public boolean handleTouch(PointF location) {
		if(isWon){
			if(quitButton.isPressed(location)) selectedButton = quitButton;
			if(retryButton.isPressed(location)) selectedButton = retryButton;
			
			return true;
		}
		if(isPaused){
			if(isGameOver){
				if(retryButton.isPressed(location)) selectedButton = retryButton;
				if(quitButton.isPressed(location)) selectedButton = quitButton;
			}
			else for(pauseButtonIterator = 0; pauseButtonIterator < pauseButtonArray.length; ++pauseButtonIterator)
				if(pauseButtonArray[pauseButtonIterator].isPressed(location))
					selectedButton = pauseButtonArray[pauseButtonIterator];
			return true;
		}
		return false;
	}
	
	public boolean handleBackPress(){
		if(isGameOver){
			kill = true;
			resume();
		}
		if(!isPaused)
			pause();
		else resume();
		return false;
	}
	
	private void reset(){
		isLoaded = false;
		isWon = false;
		isGameOver = false;
		
		resume();
		
		isLoaded = true;
	}
}